package cn.kimming.rpc.remoting.client;

import cn.kimming.rpc.common.constant.LoadBalance;
import cn.kimming.rpc.common.constant.RpcConstant;
import cn.kimming.rpc.common.constant.ProxyWay;
import cn.kimming.rpc.common.constant.SerializationWay;
import org.springframework.util.StringUtils;

public class RpcClientConfig {
    private String ip;
    private Integer port;
    private String registryUrl;
    private long timeout ;
    private SerializationWay serializationWay;
    private ProxyWay proxyWay;
    private LoadBalance loadBalance;

    public RpcClientConfig(String ip, Integer port, String registryUrl, long timeout, SerializationWay serializationWay, ProxyWay proxyWay, LoadBalance loadBalance) {
        this.ip = ip;
        this.port = port;
        this.registryUrl = registryUrl;
        this.timeout = timeout;
        this.serializationWay = serializationWay;
        this.proxyWay = proxyWay;
        this.loadBalance = loadBalance;
    }

    public String getIp() {
        return ip;
    }

    public Integer getPort() {
        return port;
    }

    public String getRegistryUrl() {
        return registryUrl;
    }

    public long getTimeout() {
        return timeout;
    }

    public SerializationWay getSerializationWay() {
        return serializationWay;
    }

    public ProxyWay getProxyWay() {
        return proxyWay;
    }

    public LoadBalance getLoadBalance() {
        return loadBalance;
    }

    public static RpcClientConfigurer configurer() {
        return new RpcClientConfigurer();
    }

    public static class RpcClientConfigurer {
        private String ip;
        private Integer port;
        private String registryUrl = RpcConstant.MY_RPC_ZK_URL_DEFAULT_VALUE;
        private long timeout = RpcConstant.MY_RPC_CALL_TIMEOUT_MS_DEFAULT_VALUE;
        private SerializationWay serializationWay = RpcConstant.MY_RPC_SERIALIZATION_WAY_DEFAULT_VALUE;
        private ProxyWay proxyWay = RpcConstant.MY_RPC_CLIENT_PROXY_WAY_DEFAULT_VALUE;
        private LoadBalance loadBalance = RpcConstant.MY_RPC_LOAD_BALANCE_DEFAULT_VALUE;

        public RpcClientConfigurer ip(String ip) {
            this.ip = ip;
            return this;
        }

        public RpcClientConfigurer port(Integer port) {
            this.port = port;
            return this;
        }

        public RpcClientConfigurer registryUrl(String registryUrl) {
            this.registryUrl = registryUrl;
            return this;
        }

        public RpcClientConfigurer timeout(long timeout) {
            this.timeout = timeout;
            return this;
        }

        public RpcClientConfigurer serializationWay(SerializationWay serializationWay) {
            this.serializationWay = serializationWay;
            return this;
        }

        public RpcClientConfigurer proxyWay(ProxyWay proxyWay) {
            this.proxyWay = proxyWay;
            return this;
        }

        public RpcClientConfigurer loadBalance(LoadBalance loadBalance) {
            this.loadBalance = loadBalance;
            return this;
        }

        public RpcClientConfig config() {
            return new RpcClientConfig(ip, port, registryUrl, timeout, serializationWay, proxyWay, loadBalance);
        }

    }

    @Override
    public String toString() {
        return "RpcClientConfig{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", registryUrl='" + registryUrl + '\'' +
                ", timeout=" + timeout +
                ", serializationWay=" + serializationWay +
                ", proxyWay=" + proxyWay +
                ", loadBalance=" + loadBalance +
                '}';
    }

    /**
     * 如果ip和port合法, 则不使用注册中心
     */
    public boolean useRegistry() {
        return !StringUtils.hasText(ip) || port <= 0;
    }
}
